import { AD_UNIT_ID_HOME, CUSTOMER_SERVICE_WECHAT, DEFAULT_ALLOW_DECIMAL } from '../../config'

Page({
  data: {
    ticketText: '',
    customerWechat: CUSTOMER_SERVICE_WECHAT,
    adUnitId: AD_UNIT_ID_HOME,
    adError: false,
    allowDecimal: DEFAULT_ALLOW_DECIMAL,
  },
  onTicketInput(e: WechatMiniprogram.TextareaInput) {
    this.setData({ ticketText: e.detail.value })
  },
  onProcess() {
    const text = this.data.ticketText.trim()
    if (!text) {
      wx.showToast({ title: '请先输入票务信息', icon: 'none' })
      return
    }

    wx.navigateTo({
      url: '/pages/result/result',
      success: (res) => {
        res.eventChannel.emit('loadData', {
          rawText: this.data.ticketText,
          allowDecimal: this.data.allowDecimal,
        })
      },
    })
  },
  onClear() {
    this.setData({ ticketText: '' })
  },
  onCopyWechat() {
    const { customerWechat } = this.data
    if (!customerWechat) {
      wx.showToast({ title: '暂未配置客服微信号', icon: 'none' })
      return
    }

    wx.setClipboardData({ data: customerWechat })
  },
  onAdError() {
    this.setData({ adError: true })
  },
  // 右上角菜单：转发到聊天
  onShareAppMessage() {
    return {
      title: '数字计算·票务文本处理',
      path: '/pages/home/home',
    }
  },
  // 右上角菜单：分享到朋友圈
  onShareTimeline() {
    return {
      title: '数字计算·票务文本处理',
    }
  },
})
